# 将图片合并成一份pdf
import os
import shutil
import sys
import fitz
import os

def main(title):
    picdir = os.path.dirname(os.path.realpath(sys.argv[0])) + '/云学堂下载数据/' + title + '/picture/'
    pdfname = os.path.dirname(os.path.realpath(sys.argv[0])) + '/云学堂下载数据/' + title + '{}.pdf'.format(title)
    pic2pdf(picdir,pdfname)



def pic2pdf(picdir,pdfname):

    total = len(os.listdir(picdir))
    doc = fitz.open()
    for i in range(1,total+1):
        img = picdir + str(i) + '.jpeg'
        imgdoc = fitz.open(img)
        pdfbytes = imgdoc.convert_to_pdf()
        imgpdf = fitz.open("pdf", pdfbytes)
        doc.insert_pdf(imgpdf)
    doc.save(pdfname)
    doc.close()
    print('PDF已经生成： PATH= ',pdfname)
    shutil.rmtree(os.path.dirname(os.path.realpath(picdir)))



